<?php

namespace App\Exports;

use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;

//新增两个 use
use Maatwebsite\Excel\Concerns\WithDrawings;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

class UsersExport implements WithDrawings
{

    /*  protected $data;

    //构造函数传值
    public function __construct($data)
    {
        $this->data = $data;
    }*/

    //数组转集合
    public function collection()
    {
        return new Collection($this->createData());
    }

    //业务代码
    public function createData()
    {
        return $this->getData();
    }
    /*默认实例
    public function collection()
    {
        return User::all();
    }
    */


    public function drawings()
    {
        $draw_arr = [1 =>'detail1.jpg', 2 => 'detail2.jpg'];
        $result = [];
        foreach ($draw_arr as $k => $v) {
            ${'drawing'.$k} = new Drawing();
            ${'drawing'.$k}->setName('Other image');
            ${'drawing'.$k}->setDescription('This is a second image');
            //图片路径
            ${'drawing'.$k}->setPath(public_path($v));
            ${'drawing'.$k}->setHeight(50);
            //设置图片列
            ${'drawing'.$k}->setCoordinates('U'.$k);
            $result[] = ${'drawing'.$k};
        }
        return $result;
    }

}
